<template>
  <!-- Excel 导入功能组件 -->
  <upload-excel :on-success="handleSuccess" />
</template>

<script>
import { formatExcelDate } from '@/utils'
import { importEmployeeAPI } from '@/api'
export default {
  name: 'Excel',
  methods: {
    // 导入成功以后的回调函数
    async handleSuccess({ results, header }) {
      console.log(results) // 完整数据
      console.log(header) // 数据头
      const obj = {
        入职日期: 'timeOfEntry',
        姓名: 'username',
        工号: 'workNumber',
        手机号: 'mobile',
        转正日期: 'correctionTime'
      }
      //   const newArr = results.map(item => {
      //     const newObj = {}
      //     for (const key in item) {
      //       newObj[obj[key]] = item[key]
      //     }
      //     return newObj
      //   })
      //   console.log(newArr)
      const newArr = results.map((item) => {
        const newobj = {}
        const arr = Object.keys(item) // [入职日期,姓名]
        arr.forEach((key) => {
          if (obj[key] === 'correctionTime' || obj[key] === 'timeOfEntry') {
            const value = formatExcelDate(item[key])
            newobj[obj[key]] = value
          } else {
            newobj[obj[key]] = item[key]
          }
        })
        return newobj
      })
      console.log(newArr)
      const res = await importEmployeeAPI(newArr).catch((err) => err)
      if (res.success) this.$message.success(res.message)
      this.$router.back()
    }
  }
}
</script>

<style lang="scss" scoped></style>
